// grass/pages/grassRecord/grassRecord.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        recordList: [],
        showTip: false,
        currentIndex: 0,
        ruleModal: false,
        recommend: 0,
        //奖励refuseModal
        swiper: {
            indicatorDots: true,
            vertical: false,
            autoplay: true,
            interval: 2e3,
            duration: 500
        },
        refuseReason: "",
        refuseModal: false,
        awardList: []
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        that.requestStart();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.requestStart();
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    requestStart: function() {
        let that = this;
        that.getRecord();
        //上传记录列表
                that.getAward_rule();
        //奖励规则
                that.getAwardList();
        //是否自动领取奖励
                that.draftList();
        //草稿箱数据
        },
    //全局基础配置信息
    getBase: function(openid) {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    color_header: res.extra.color_header,
                    color_text: res.extra.color_text,
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    style: res.extra.style
                });
            }
        });
    },
    operate: function(e) {
        let that = this;
        that.setData({
            showTip: true,
            currentIndex: e.currentTarget.dataset.index
        });
    },
    hideShowTip: function() {
        let that = this;
        that.setData({
            showTip: false
        });
    },
    /**
   * 种草列表
   */
    getRecord: function() {
        let that = this;
        common.request({
            endpoint: "Plantgrass/person/record",
            method: "post",
            data: {},
            success: res => {
                let recordList = res.extra;
                recordList.map(x => {
                    if (x.img) {
                        x.img = x.img.split(",");
                    }
                });
                that.setData({
                    recordList: recordList
                });
            }
        });
    },
    /**
   * 是否自动领取奖励
   */
    getAwardList: function() {
        let that = this;
        common.request({
            endpoint: "Plantgrass/person/award_list",
            method: "post",
            data: {},
            success: res => {
                that.setData({
                    awardList: res.extra
                });
            }
        });
    },
    /**
   * 获取奖励
   */
    getAward: function(e) {
        let that = this;
        common.request({
            endpoint: "Plantgrass/person/get_award",
            method: "post",
            data: {
                id: e.currentTarget.dataset.id
            },
            success: res => {
                let awardList = [];
                awardList = [ res.extra ];
                that.setData({
                    awardList: awardList
                });
            }
        });
    },
    /**
   * 获取奖励详情
   */
    getAwardDetail: function(e) {
        let that = this;
        common.request({
            endpoint: "Plantgrass/person/each_detail",
            method: "post",
            data: {
                id: e.currentTarget.dataset.id
            },
            success: res => {
                let awardList = [];
                awardList = [ res.extra ];
                that.setData({
                    awardList: awardList
                });
            }
        });
    },
    hideAwardModal: function() {
        let that = this;
        that.setData({
            awardList: []
        });
        that.getRecord();
        //上传记录列表
        },
    /**
   * 草稿箱数据
   */
    draftList: function() {
        let that = this;
        common.request({
            endpoint: "Plantgrass/person/draft",
            method: "post",
            data: {},
            success: res => {
                let draftList = res.extra;
                that.setData({
                    draftList: draftList
                });
            }
        });
    },
    /**
   * 种草奖励规则
   */
    getAward_rule: function() {
        let that = this;
        common.request({
            endpoint: "Plantgrass/person/award_rule",
            method: "post",
            data: {},
            success: res => {
                that.setData({
                    rule: res.extra
                });
            }
        });
    },
    showRuleModal: function() {
        let that = this;
        that.setData({
            ruleModal: true
        });
    },
    hideRuleModal: function() {
        let that = this;
        that.setData({
            ruleModal: false
        });
    },
    goToDetail(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    },
    /**
   * 跳转视频或者图文详情页
   */
    toDetail: function(e) {
        let type = e.currentTarget.dataset.item.type;
        if (type == 1) {
            //图文
            wx.navigateTo({
                url: "/grass/pages/grassDetail/grassDetail?id=" + e.currentTarget.dataset.item.id
            });
        } else {
            wx.navigateTo({
                url: "/grass/pages/grassVideo/grassVideo?id=" + e.currentTarget.dataset.item.id
            });
        }
    },
    /**
   * 删除种草
   */
    deleteGrass: function(e) {
        let that = this;
        wx: wx.showModal({
            title: "提示",
            content: "是否删除该种草!",
            success: function(res) {
                if (res.confirm) {
                    common.request({
                        endpoint: "Plantgrass/person/delete_draft",
                        method: "post",
                        data: {
                            id: e.currentTarget.dataset.id
                        },
                        success: res => {
                            wx.showModal({
                                title: "提示",
                                content: "删除成功!",
                                showCancel: false,
                                success: function() {
                                    that.setData({
                                        showTip: false
                                    });
                                    that.getRecord();
                                }
                            });
                        }
                    });
                }
            }
        });
    },
    /**拒绝详情 */
    showRefuseModal: function(e) {
        let that = this;
        that.setData({
            refuseModal: true,
            refuseReason: e.currentTarget.dataset.reason ? e.currentTarget.dataset.reason : "无"
        });
    },
    hideRefuseModal: function() {
        let that = this;
        that.setData({
            refuseModal: false
        });
    }
});